﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModels;

namespace DAL
{
    /// <summary>
    /// 图书管理数据访问类
    /// </summary>
    public class BookDAL
    {
        SqlCommand cmd;//定义Command对象
        public BookDAL()
        {
            cmd = new SqlCommand();//实例化Command对象
            cmd.CommandType = CommandType.StoredProcedure;//定义Command对象实现使用存储过程
        }
        /// <summary>
        /// 获取书籍列表
        /// </summary>
        /// <returns>ds</returns>
        public DataSet GetBookList()
        {
            cmd.CommandText = "GetBookList";
            cmd.Parameters.Clear();//清空Command的参数内容
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public DataSet GetBookListByID(string inputstring)
        {
            cmd.CommandText = "GetBookListByID";//指定存储过程名称
            cmd.Parameters.Clear();//清空Command的参数内容
            // 传参实现
            cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 20).Value = inputstring;
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public DataSet GetBookListByName(string inputstring)
        {
            cmd.CommandText = "GetBookListByName";//指定存储过程名称
            cmd.Parameters.Clear();//清空Command的参数内容
            // 传参实现
            cmd.Parameters.Add("@bookname", SqlDbType.NVarChar, 200).Value = inputstring;
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public DataSet GetBookListByAuthor(string inputstring)
        {
            cmd.CommandText = "GetBookListByAuthor";//指定存储过程名称
            cmd.Parameters.Clear();//清空Command的参数内容
            // 传参实现
            cmd.Parameters.Add("@author", SqlDbType.NVarChar, 50).Value = inputstring;
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }

        public DataSet GetBookClasses()
        {
            cmd.CommandText = "GetBookClasses";//指定存储过程名称
            cmd.Parameters.Clear();//清空Command的参数内容
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public string GetBookClassName(string classid)
        {
            cmd.CommandText = "GetBookClassByID";//指定存储过程名称
            cmd.Parameters.Clear();//清空Command的参数内容
            // 传参实现
            cmd.Parameters.Add("@classid", SqlDbType.VarChar, 10).Value = classid;

            object obj = SQLHelper.GetScalar(cmd);

            return obj.ToString();
        }
        public bool ChecingDuplication(BookVM vm)
        {
            cmd.CommandText = "SearchBookByBookID";//指定存储过程名称
            cmd.Parameters.Clear();//清空Command的参数内容
            // 传参实现
            cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 20).Value = vm.BookID;

            object obj = SQLHelper.GetScalar(cmd);
            if (obj != null)
                return true;
            else
                return false;

        }
        public object EditBook(BookVM vm, string operation)
        {
            if (operation == "I")
                cmd.CommandText = "InsertBook";//当传入标识为I，进行书籍新增
            if (operation == "U")
                cmd.CommandText = "UpdateBook";//当传入标识为U，进行书籍修改
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@BookID", SqlDbType.VarChar, 20).Value = vm.BookID;
            cmd.Parameters.Add("@BookName", SqlDbType.NVarChar, 200).Value = vm.BookName;
            cmd.Parameters.Add("@Author", SqlDbType.NVarChar, 50).Value = vm.Author;
            cmd.Parameters.Add("@Publisher", SqlDbType.NVarChar, 50).Value = vm.Publisher;
            cmd.Parameters.Add("@ISBN", SqlDbType.Char, 20).Value = vm.ISBN;
            cmd.Parameters.Add("@Prices", SqlDbType.Money).Value = vm.Price;
            cmd.Parameters.Add("@WordCount", SqlDbType.Int).Value = vm.WordCount;
            cmd.Parameters.Add("@PageCount", SqlDbType.Int).Value = vm.PageCount;
            cmd.Parameters.Add("@ClassID", SqlDbType.VarChar, 10).Value = vm.ClassID;
            cmd.Parameters.Add("@PublishDate", SqlDbType.DateTime).Value = vm.PublishDate;
            cmd.Parameters.Add("@BookVersion", SqlDbType.NVarChar, 40).Value = vm.BookVersion;
            try
            {
                int num = SQLHelper.ExcuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public DataSet GetBookByID(string bookid)
        {
            cmd.CommandText = "GetBookInfoByBookID";//指定调用存储过程名称
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 20).Value = bookid;
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public object DelBook(string bookid)
        {
            cmd.CommandText = "DeleteBookByBookID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 20).Value = bookid;
            try
            {
                int num = SQLHelper.ExcuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
